Find the first digit of an integer. Start count digits from the leftmost
one.
Input. One 64-bit integer
containing at least one digits. The number can be negative.
Output. Print the first digit
of a given integer.
Sample input 1 |
Sample output 1 |
1234567890123 |
1 |
|
|
Sample input 2 |
Sample output 2 |
-7654321 |
7 |
loop
If number
is negative, then change its sign, making it positive – this will not change the first digit. Divide number by 10 until one digit remains in it – this will be the first digit of the original number.
Read the input value of n.
scanf("%lld",&n);
If the number is negative, then make it positive.
if (n < 0) n = -n;
Divide number by 10 until it contains only the first
digit.
while(n > 9)
n /= 10;
Print the first
digit of number.
printf("%lld\n",n);
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
long n = con.nextLong();
if (n < 0) n = -n;
while(n > 9)
n /= 10;
System.out.println(n);
con.close();
}
}